package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.bean.ClassifyGiftSecond;
import com.ailiwu.www.ailiwu.bean.HotCommodityData;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

/**
 * Author:    Yyang
 * Version    V1.0
 * Date:      ${date}  ${time}.
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * ${date}          Yyang                1.0                1.0
 * Why & What is modified:
 */
public class HotRecyclerViewAdapter extends
        RecyclerView.Adapter<HotRecyclerViewAdapter.MyViewHolder> {
    private Context context;
    private LayoutInflater layoutInflater;
    private ArrayList<HotCommodityData.DatasEntity.ItemsEntity.DataEntity> data;
    private ArrayList<ClassifyGiftSecond.DataEntity.ItemsEntity> Datas;
    private StringBuilder sb = new StringBuilder();
    private DisplayImageOptions options;
    private ImageLoader imageLoader;

    public HotRecyclerViewAdapter(Context context, ArrayList<HotCommodityData.DatasEntity.ItemsEntity.DataEntity> data) {
        this.context = context;
        this.data = data;
        layoutInflater = LayoutInflater.from(context);

    }
    public HotRecyclerViewAdapter(Context context, Object data) {
        this.context = context;
        this.Datas = (ArrayList<ClassifyGiftSecond.DataEntity.ItemsEntity>) data;
        layoutInflater = LayoutInflater.from(context);
    }


    // 创建ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_recycler_commodity, parent, false);
        return new MyViewHolder(view, itemClickListener);
    }

    //进行数据的动态设置
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ig_holder_image_large)//设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.drawable.ig_holder_image_large)//设置图片Uri为空或是错误的时候显示的图片
                .bitmapConfig(Bitmap.Config.RGB_565)                // 设置图片的解码类型
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)   // 设置图片以如何的编码方式显示
                .cacheInMemory(true)                                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                                  // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(10))          // 设置成圆角图片
                .build();  // 创建配置过得DisplayImageOption对象                                        // 创建配置过得DisplayImageOption对象
        if (data != null) {
            holder.tv_name.setText(data.get(position).name);
            holder.tv_price.setText(data.get(position).price);
            holder.tv_favorites_count.setText(
                    getFavoritesCount(data.get(position).favorites_count));
            imageLoader.displayImage(data.get(position).cover_image_url, holder.cover_image, options);
        } else {
            holder.tv_name.setText(Datas.get(position).getName());
            holder.tv_price.setText(Datas.get(position).getPrice());
            holder.tv_favorites_count.setText(
                    getFavoritesCount(Datas.get(position).getFavorites_count()));

            imageLoader.displayImage(Datas.get(position).getCover_image_url(), holder.cover_image, options);
        }


    }
    @Override
    public int getItemCount() {
        if(data != null){
            return data == null ? 0 : data.size();
        }else{
            return Datas == null ? 0 : Datas.size();
        }

    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView cover_image;
        private TextView tv_name, tv_price, tv_favorites_count;
        private ItemClickListener ItemClickListener = null;

        public MyViewHolder(View itemView, ItemClickListener ItemClickListener) {
            super(itemView);
            this.ItemClickListener = ItemClickListener;  //监听的具体事件通过Adapter创建ViewHolder时传递进来

            itemView.setOnClickListener(this);           // 对每个itemView设置点击监听

            cover_image = (ImageView) itemView.findViewById(R.id.cover_image);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_favorites_count = (TextView) itemView.findViewById(R.id.tv_favorites_count);
        }

        @Override
        public void onClick(View v) {
            if (ItemClickListener != null) {
                ItemClickListener.onItemClick(v, getPosition());
            }
        }
    }

    // 点击监听接口
    public interface ItemClickListener {
        void onItemClick(View view, int postion);
    }

    private ItemClickListener itemClickListener;

    /**
     * 设置监听事件具体内容的方法
     *
     * @param listener 监听事件具体内容
     */
    public void setItemClickListener(ItemClickListener listener) {
        this.itemClickListener = listener;
    }

    /**
     * 格式喜欢的数量00.0k
     *
     * @param favorites_count 喜欢数量
     * @return 格式化后的喜欢数量字符串
     */
    public String getFavoritesCount(int favorites_count) {
        if (favorites_count < 1000) {
            return favorites_count + "";
        } else {
            sb.delete(0, sb.length());
            String thousand = favorites_count / 1000 + "";
            sb.append(thousand + ".");
            String hundred = favorites_count % 1000 / 100 + "";
            sb.append(hundred + "k");
            return sb.toString();
        }


    }
}
